package com.xlh.controller.course;

import com.xlh.common.Result;
import com.xlh.dto.course.ZtreeDTO;
import com.xlh.param.course.CourseDirectionParam;
import com.xlh.param.course.DirectionParam;
import com.xlh.service.course.DirectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程方向
 * <p>
 * Created by lx on 2019/12/25.
 */
@Api(tags = "DirectionController", description = "课程方向")
@RestController
@RequestMapping("/direction")
@PreAuthorize("@permissionValidator.isAdmin()")
public class DirectionController {

    @Autowired
    private DirectionService directionService;

    @ApiOperation("查询课程方向所有数据")
    @GetMapping("/list")
    public Result<List<ZtreeDTO>> listDirection(
            @RequestParam @ApiParam(value = "学校类型", required = true) Integer type) {
        return Result.success(directionService.listDirection(type));
    }

    @ApiOperation("添加/修改/删除课程方向")
    @PostMapping()
    public Result createDirection(
            @RequestParam @ApiParam(value = "学校类型", required = true) Integer type,
            @RequestBody @ApiParam(value = "课程方向的参数", required = true) @Valid List<DirectionParam> params) {
        directionService.createDirection(type, params);
        return Result.success();
    }

    @ApiOperation("保存课程及专业方向的关联关系")
    @PostMapping("/course")
    public Result createCourseExtend(
            @RequestBody @ApiParam(value = "保存参数", required = true) @Valid List<CourseDirectionParam> params) {
        directionService.createCourseExtend(params);
        return Result.success();
    }

    @ApiOperation("查看专业方向的关联关系")
    @GetMapping("/ztree")
    @PreAuthorize("@permissionValidator.isAdminOrTeacher()")
    public Result<List<ZtreeDTO>> queryDirectionZtree(
            @RequestParam(required = false) @ApiParam(value = "学校类型集合") List<Integer> types,
            @RequestParam(required = false, defaultValue = "false")
            @ApiParam(value = "是否查看自己（教师）的权限") Boolean ownAuthorityFlag) {
        return Result.success(directionService.queryDirectionZtree(types, ownAuthorityFlag));
    }

    @ApiOperation("下载某专业方向的课程及章节的汇总信息")
    @PostMapping("/{id}/download")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result download(@PathVariable Long id) {
        directionService.download(id);
        return Result.success();
    }

}
